﻿using PIMS.Models.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PIMS.Utils
{
    internal class DatatableToEntity
    {
        /// <summary>  
        /// 填充对象列表：用DataTable填充实体类
        /// </summary>  
        public static List<T> FillModel<T>(DataTable dt) where T : new()
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            List<T> modelList = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T model = new T();
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                    if (propertyInfo != null && dr[i] != DBNull.Value)
                    {
                        string value = dr[i] == null ? "" : dr[i].ToString();
                        propertyInfo.SetValue(model, value, null);
                    }
                }
                modelList.Add(model);
            }
            return modelList;
        }

        /// <summary>  
        /// 填充对象：用DataRow填充实体类
        /// </summary>  
        public static T FillModel<T>(DataRow dr) where T : new()
        {
            if (dr == null)
            {
                return default(T);
            }

            T model = new T();
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (propertyInfo != null && dr[i] != DBNull.Value)
                {
                    string value = dr[i] == null ? "" : dr[i].ToString();
                    // propertyInfo.SetValue(model, value, null);
                    string typestr = dr[i].GetType().Name;
                    if (typestr.Equals("Int32"))
                    {
                        propertyInfo.SetValue(model, Convert.ToInt32(value), null);
                    }
                    else if (typestr.Equals("Int64"))
                    {
                        propertyInfo.SetValue(model, Convert.ToInt64(value), null);
                    }
                    else
                    {
                        propertyInfo.SetValue(model, value, null);
                    }
                }
            }
            return model;
        }


        public static T FillModel<T>(DataRow dr, FeatureInfoX x) where T : new()
        {
            if (dr == null)
            {
                return default(T);
            }

            Dictionary<string, string> myDict = new Dictionary<string, string>();
            foreach (XItems s in x.item)
            {
                if (s.enable)
                {
                    myDict.Add(s.dispName,s.name);
                }
            }

            T model = new T();
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                PropertyInfo propertyInfo = model.GetType().GetProperty(myDict[dr.Table.Columns[i].ColumnName], BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (propertyInfo != null && dr[i] != DBNull.Value)
                {
                    string value = dr[i] == null ? "" : dr[i].ToString();
                    string typestr = dr[i].GetType().Name;
                    if (typestr.Equals("Int32"))
                    {
                        propertyInfo.SetValue(model, Convert.ToInt32(value), null);
                    }
                    else if (typestr.Equals("Int64"))
                    {
                        propertyInfo.SetValue(model, Convert.ToInt64(value), null);
                    }
                    else
                    {
                        propertyInfo.SetValue(model, value, null);
                    }
                }
            }
            return model;
        }
    }
}
